package com.imooc.miaosha.web;

import com.imooc.miaosha.result.CodeMsg;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.service.UserService;
import com.imooc.miaosha.util.ValidatorUtil;
import com.imooc.miaosha.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * @author simple
 * @version 1.0
 * @date 2018/6/15 22:40
 */
@Slf4j
@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("")
    public String login() {
        log.debug("someone visit login page");
        return "login";
    }

    @RequestMapping("log")
    @ResponseBody
    public Result log(@Valid LoginVo loginVo) {
        if (null == loginVo) {
            return Result.error(CodeMsg.LOGIN_VO_NULL);
        }
        log.info(loginVo.toString());
//        if (StringUtils.isBlank(loginVo.getMobile())) {
//            return Result.error(CodeMsg.MOBILE_NULL);
//        }
//        if (StringUtils.isBlank(loginVo.getPassword())) {
//            return Result.error(CodeMsg.PASSWORD_NULL);
//        }
//        if (ValidatorUtil.isNotMobile(loginVo.getMobile())) {
//            return Result.error(CodeMsg.MOBILE_FORMAT_ERROR);
//        }
        Result result = userService.log(loginVo);
        log.info(result.toString());
        return result;
    }
}
